//
//  DetailHeaderTableViewCell.m
//  MyBlog
//
//  Created by NCS on 16/12/21.
//  Copyright © 2016年 YSL. All rights reserved.
//

#import "DetailHeaderTableViewCell.h"

static NSString *CollectionViewCellIdentifier = @"homeCellCollectionCell";
const CGFloat GAP_IN_CELL = 55;

@implementation DetailHeaderTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // Register nib file for the cell
    UINib *nib = [UINib nibWithNibName:@"CollectionCell" bundle: [NSBundle mainBundle]];
    [self.images registerNib:nib forCellWithReuseIdentifier:CollectionViewCellIdentifier];
    
    self.images.backgroundColor = [UIColor clearColor];
    
    imageSize = DScreenWidth / 3 - 10;
    
//    [self updateConstraints];
    
    self.content.numberOfLines = 0;
//    self.content.lineBreakMode = NSLineBreakByCharWrapping;
//
//    [self.content sizeThatFits:CGSizeMake(self.content.frame.size.width, MAXFLOAT)];
    
    // Config CollectionView delegate and data source
    self.images.delegate = self;
    self.images.dataSource = self;

}

- (void)updateConstraints {
    [super updateConstraints];
    
    long imagesCount = [self.modal.images count];
    
    if (imagesCount == 0) {
        self.imagesHeightConstraint.constant = 0;
    } else if (imagesCount >= 1 && imagesCount <= 3){
        self.imagesHeightConstraint.constant = imageSize    ;
    } else if (imagesCount > 3 && imagesCount <= 6){
        self.imagesHeightConstraint.constant = imageSize * 2 + COLLECTION_GAP;
    } else if (imagesCount > 6 && imagesCount <= 9){
        self.imagesHeightConstraint.constant = imageSize  * 3 + COLLECTION_GAP * 3;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setCellData:(HomeCellModel *)model {
    self.modal = [[HomeCellModel alloc] initWithModel:model];
    [self updateConstraints];
}

- (CGFloat)getCellHeight {
    if (self.modal == nil) {
        return 0;
    }
    
    CGFloat contentHeight = [DetailHeaderTableViewCell getContentHeightByWidth:DScreenWidth Title:self.content.text font:[UIFont systemFontOfSize:17]];
    
    // otherHeight = avatarHeight + gap
    CGFloat otherHeight = self.avatar.bounds.size.height + GAP_IN_CELL;
    
    self.cellHeight = contentHeight + self.imagesHeightConstraint.constant + otherHeight;
    
    self.modal.cellHeight = self.cellHeight;
    return self.cellHeight;
}

+ (CGFloat)getContentHeightByWidth:(CGFloat)width Title:(NSString *)title font:(UIFont *)font {
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height;
}

#pragma mark -- UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return [self.modal.images count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // Set up the reuse identifier
    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CollectionViewCellIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
    
    [[SDWebImageDownloader sharedDownloader] setValue:@"iPhone" forHTTPHeaderField:@"User-Agent"];
    
    NSURL *url = [NSURL URLWithString:self.modal.images[indexPath.row]];
    [cell setCellImageWithURL:url];
    
    return cell;
    
}

#pragma mark -- Collection delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CollectionCell *cell = (CollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    [YSLScanImage scanBigImageWithImageView:cell.collectionImage];
}

#pragma mark -- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // Change the size of each item in the collectionView
    return CGSizeMake(imageSize, imageSize);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return COLLECTION_GAP;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2;
}
@end
